import { app } from "db://app-game/core/app";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { GameEvent } from "../../../common/config/GameEvent";
import { AccountNetDataComp } from "./bll/AccountNetDataSystem";
import { AccountModelComp } from "./model/AccountModelComp";

/** 账户管理 */
@ecs.register("Account")
export class Account extends ecs.Entity {
  AccountModel!: AccountModelComp;
  AccountNetData!: AccountNetDataComp;
  protected init() {
    this.addComponents<ecs.Comp>(AccountModelComp);
    this.addEvent();
  }

  destroy(): void {
    this.removeEvent();
    super.destroy();
  }

  /** 添加全局消息事件 */
  private addEvent() {
    app.message.on(GameEvent.GameServerConnected, this.onHandler, this);
  }

  /** 移除全局消息事件 */
  private removeEvent() {
    app.message.off(GameEvent.GameServerConnected, this.onHandler, this);
  }

  private onHandler(event: string, args: any) {
    switch (event) {
      case GameEvent.GameServerConnected:
        this.getPlayer();
        break;
    }
  }

  /** 连接游戏服务器 */
  connect() {
    // 无网状态下测试代码，有网络时会通过触发网络连接成功事件对接后续流程TODO
    app.message.dispatchEvent(GameEvent.GameServerConnected);
  }

  /** 获取玩家信息 */
  getPlayer() {
    this.add(AccountNetDataComp);
  }
}
